package com.mes.spc.rule;

import java.util.ArrayList;
import java.util.List;

public class SPCCommand {
    
    public static SPCCommandbase SPCRule1 = new SpcRule1();
    public static SPCCommandbase SPCRule2 = new SpcRule2();
    public static SPCCommandbase SPCRule3 = new SpcRule3();
    public static SPCCommandbase SPCRule4 = new SpcRule4();
    public static SPCCommandbase SPCRule5 = new SpcRule5();
    public static SPCCommandbase SPCRule6 = new SpcRule6();
    public static SPCCommandbase SPCRule7 = new SpcRule7();
    public static SPCCommandbase SPCRule8 = new SpcRule8();
    
    private static class SpcRule1 extends SPCCommandbase
    {
        public int id = 1;
        public int warningCount = 1;
        public String title = "规则1";
        public String description = "1个点落在A区以外"; 
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}

		public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            if (data > ucl+standard || data < lcl+standard)
                return true;
            return false;
        }
    }
    private static class SpcRule2 extends SPCCommandbase
    {
        public int id = 2;
        public int warningCount = 9; 
        public String title = "规则2";
        public String description= "连续9个点落在中心线同一侧";
        
        private int count = 0;
        private Boolean up = true;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            if(data==standard)
            {
                up = null;
                count = 0;
                return false;
            }
            boolean temp = data > standard;
            if (count > 0 && temp == up)
            {
                count++;
                if (count >= 9)
                    return true;
            }
            else
            {
                up = temp;
                count = 1;
            }
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule3 extends SPCCommandbase
    {
        public int id = 3;
        public int warningCount = 6;
        public String title = "规则3";
        public String description = "连续6个点递增或递减";
        private int count = 0;
        private double last = 0;
        private Boolean up = true;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            if(data == last)
            {
                last = data;
                up = null;
                count = 0;
                return false;
            }
            boolean temp = data > last;
            if (count > 0 && temp == up)
            {
                count++;
                last = data;
                if (count >= 6)
                    return true;
            }
            else
            {
                up = temp;
                count = 1;
                last = data;
            }
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule4 extends SPCCommandbase
    {
        public int id = 4;
        public int warningCount=14;
        public String title="规则4";
        public String description = "连续14个点中相邻点交替上下";
        private int count = 0;
        private double last = 0;
        private Boolean up = true;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            if (data == last)
            {
                last = data;
                up = null;
                count = 0;
                return false;
            }
            boolean temp = (data > last);
            if (count > 0 && temp != up)
            {
                count++;
                last = data;
                up = temp;
                if (count >= 14)
                    return true;
            }
            else
            {
                up = temp;
                count = 1;
                last = data;
            }
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule5 extends SPCCommandbase
    {
        public int id=5;
        public int warningCount=3;
        public String title="规则5";
        public String description="连续3个点中有2个点落在中心线同一侧的B区以外";
        private List<Double> old = new ArrayList<Double>();
        private int count;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            double bu =standard+ ucl/ 3 * 2;
            double bl =standard+ lcl / 3 * 2;
            int u = 0;
            int l = 0;
            old.add(data);
            count++;
            if (count > 3)
            {
                count--;
                old.remove(0);
            }
            for(double o : old)
            {
                if (o > bu)
                    u++;
                else if (o < bl)
                    l++;
            }
            if (u >= 2 || l >= 2)
                return true;
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule6 extends SPCCommandbase
    {
        public int id =6;
        public int warningCount =5;
        public String title = "规则6";
        public String description="连续5个点中有4个点落在中心线同一侧的C区以外";
        private List<Double> old = new ArrayList<Double>();
        private int count;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            double bu = standard + ucl / 3;
            double bl = standard + lcl / 3;
            int u = 0;
            int l = 0;
            old.add(data);
            count++;
            if (count > 5)
            {
                count--;
                old.remove(0);
            }
            for(double o : old)
            {
                if (o > bu)
                    u++;
                else if (o < bl)
                    l++;
            }
            if (u >= 4 || l >= 4)
                return true;
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule7 extends SPCCommandbase
    {
        public int id=7;
        public int warningCount=15;
        public String title="规则7";
        public String description="连续15个点落在中心线两侧的C区内";
        private int count;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            double bu =standard+ ucl / 3;
            double bl = standard + lcl / 3;
            if (data < bu && data > bl)
                count++;
            else
                count = 0;
            if (count >= 15)
                return true;
            return false;
        }
        
        public int getId() {
			return id;
		}

		public int getWarningCount() {
			return warningCount;
		}

		public String getTitle() {
			return title;
		}

		public String getDescription() {
			return description;
		}
    }
    private static class SpcRule8 extends SPCCommandbase
    {
        public int id=8;
        public int warningCount=8;
        public String title="规则8";
        public String description="连续8个点落在中心线两侧且无一在C区内";
        private int count;
        public boolean excute(double data, double usl, double lsl, double ucl, double lcl, double standard)
        {
            double bu = standard + ucl/ 3;
            double bl = standard + lcl / 3;
            if (data > bu || data < bl)
                count++;
            else
                count = 0;
            if (count >= 8)
                return true;
            return false;
        }
        
        public int getId() {
			return id;
		}
		public int getWarningCount() {
			return warningCount;
		}
		public String getTitle() {
			return title;
		}
		public String getDescription() {
			return description;
		}
    }
}
